package org.csu.jpetstorespringversion.model;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Data
@Repository
@TableName(value = "orders")
public class Order {

    private static final long serialVersionUID = 6321792448424424931L;

    //等同于ID_ WORKER(3) ,ID_ WORKER STR(3) ，使用mp自动赋值 采取雪花算法,但是MyBatisPlus3.20的时候是采取该字段最大值+1
    @TableId(value = "orderid", type = IdType.AUTO)
    private int orderId;
    @TableField(value = "userid")
    private String username;
    @TableField(value = "orderdate")
    private Date orderDate;
    @TableField(value = "shipaddr1")
    private String shipAddress1;
    @TableField(value = "shipaddr2")
    private String shipAddress2;
    @TableField(value = "shipcity")
    private String shipCity;
    @TableField(value = "shipstate")
    private String shipState;
    @TableField(exist = true,value = "shipzip")//表示数据库表中存在这个字段
    private String shipZip;
    @TableField(value = "shipcountry")
    private String shipCountry;
    @TableField(value = "billaddr1")
    private String billAddress1;
    @TableField(value = "billaddr2")
    private String billAddress2;
    @TableField(value = "billcity")
    private String billCity;
    @TableField(value = "billzip")
    private String billZip;
    @TableField(value = "billcountry")
    private String billCountry;

    @TableField(value = "billstate")
    private String billState;

    private String courier;
    @TableField(value = "totalprice")
    private BigDecimal totalPrice;
    @TableField(value = "billtofirstname")
    private String billToFirstName;
    @TableField(value = "billtolastname")
    private String billToLastName;
    @TableField(value = "shiptofirstname")
    private String shipToFirstName;
    @TableField(value = "shiptolastname")
    private String shipToLastName;
    @TableField(value = "creditcard")
    private String creditCard;
    @TableField(value = "exprdate")
    private String expiryDate;
    @TableField(value = "cardtype")
    private String cardType;
    @TableField(value = "locale")
    private String locale;
    @TableField(exist = false)
    private String status;
    @TableField(value = "state")
    private String state;
    @TableField(value = "sendout")
    private String sendout;
    @TableField(exist = false)
    private List<LineItem> lineItems = new ArrayList<LineItem>();

    public void initOrder(Account account, Cart cart) {

        username = account.getUserid();
        if (username == null){
            System.out.println("username is null when initorder");
        }else{
            System.out.println("username is not null when initorder");
        }
        orderDate = new java.sql.Date(System.currentTimeMillis());

        shipToFirstName = account.getFirstname();
        shipToLastName = account.getLastname();
        shipAddress1 = account.getAddr1();
        shipAddress2 = account.getAddr2();
        shipCity = account.getCity();
        shipState = account.getState();
        shipZip = account.getZip();
        shipCountry = account.getCountry();

        billToFirstName = account.getFirstname();
        billToLastName = account.getLastname();
        billAddress1 = account.getAddr1();
        billAddress2 = account.getAddr2();
        billCity = account.getCity();
        billState = account.getState();
        billZip = account.getZip();
        billCountry = account.getCountry();

        totalPrice = cart.getSubTotal();
        state="未付款";
        sendout="未发货";

        /**这个肯定也得修改，这个是他默认的银行卡信息**/
        creditCard = "999 9999 9999 9999";
        expiryDate = "12/03";
        cardType = "Visa";
        courier = "UPS";
        locale = "CA";
        status = "P";

        //这条语句就是把cart中有的item全部添加到lineitem列表中去
        Iterator<CartItem> i = cart.getAllCartItems();
        while (i.hasNext()) {
            CartItem cartItem = (CartItem) i.next();
            addLineItem(cartItem);
        }

    }

    public void addLineItem(CartItem cartItem) {
        LineItem lineItem = new LineItem(lineItems.size() + 1, cartItem);
        addLineItem(lineItem);
    }

    public void addLineItem(LineItem lineItem) {
        lineItems.add(lineItem);
    }

}
